<script setup>
import { artDelChannelService, artGetChannelsService } from '@/api/article'
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelEdit from './components/ChannelEdit.vue'

const channelList = ref([])
const loading = ref(false)
const dialog = ref()
async function getChannelList() {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  // channelList.value = []
  loading.value = false
}
getChannelList()
function onEditChannel(row) {
  dialog.value.open(row)
}
async function onDelChannel(row) {
  // eslint-disable-next-line no-undef
  await ElMessageBox.confirm('你确认要删除吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
  await artDelChannelService(row.id)
  // eslint-disable-next-line no-undef
  ElMessage.success('删除成功')
  getChannelList()
}
function onAddChannel() {
  dialog.value.open({})
}
function onSuccess() {
  getChannelList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel">
        添加分类
      </el-button>
    </template>
    <el-table v-loading="loading" :data="channelList" border stripe style="width: 100%;">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column label="分类名称" prop="cate_name" />
      <el-table-column label="分类别名" prop="cate_alias" />
      <el-table-column label="操作" width="150">
        <!-- row就是每一项，$index就是下标 -->
        <template #default="{ row, $index }">
          <el-button plain circle type="primary" :icon="Edit" @click="onEditChannel(row, $index)" />
          <el-button plain circle type="danger" :icon="Delete" @click="onDelChannel(row, $index)" />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess" />
  </page-container>
</template>

<style lang="scss" scoped>

</style>
